package com.sa07.config;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;

import com.sa07.bean.SystemConstant;
import com.sa07.bean.TestItemBean;
import com.sa07.bean.TestItemConstTag;
import com.sa07.bean.TestSubItem;

/**
 * 测试项初始化
 * 
 * @author Long.shangbo
 *
 * 2014-2-22 下午11:39:37
 */
public class TestItemInit extends XmlConfigInit {
	
	/**
	 * 测试项配置文件
	 */
	private static final String TEST_ITEM_CONFIG_FILE = "test_item_config.xml";

	@Override
	protected InputStream getConfigInputStream() throws FileNotFoundException {
		return new BufferedInputStream(new FileInputStream(
				SystemConstant.CONFIG_DIR + File.separator
						+ TEST_ITEM_CONFIG_FILE));
	}

	@Override
	protected void analyzeDocment(Document doc) {
		Element root = doc.getRootElement();
		List<TestItemBean> testItems = new ArrayList<TestItemBean>();
		
		if(root != null){
			List<Element> testEles = root.getChildren(TestItemConstTag.TEST_ITEM_TAG);
			
			if(testEles != null && !testEles.isEmpty()){
				TestItemBean testItem = null;
				Element testEle = null;
				Element subTestEle = null;
				TestSubItem subItem = null;
				List<Element> subEles = null;
				TestSubItem previousSubItem = null;
				List<TestSubItem> subItems = null;
				
				for(int i = 0, len = testEles.size(); i < len; i++){
					testEle = testEles.get(i);
					
					if(testEle != null){
						testItem = new TestItemBean();
						testItem.setTestItemId(i + 1);
						testItem.setTestItemName(testEle.getAttributeValue(TestItemConstTag.NAME_ATTR));
						testItem.setTestItemType(Integer.parseInt(testEle.getAttributeValue(TestItemConstTag.TYPE_ATTR)));
						testItem.setTestTimeout(Long.parseLong(testEle.getAttributeValue(TestItemConstTag.TIMEOUT_ATTR)));
						
						subEles = testEle.getChildren(TestItemConstTag.SUB_ITEM_TAG);
						subItems = new ArrayList<TestSubItem>();
						
						if(subEles != null && !subEles.isEmpty()){
							for(int j = 0, subLen = subEles.size(); j < subLen; j++){
								subTestEle = subEles.get(j);
								
								if(subTestEle != null){
									subItem = new TestSubItem();
									subItem.setSubItemId(j + 1);
									subItem.setSubItemName(subTestEle.getAttributeValue(TestItemConstTag.NAME_ATTR));
									subItem.setItemCmd(Integer.parseInt(subTestEle.getAttributeValue(TestItemConstTag.CMD_ATTR)));
									subItem.setContent(subTestEle.getAttributeValue(TestItemConstTag.CONTENT_ATTR));
									subItem.setPreviousSubItem(previousSubItem);
									previousSubItem = subItem;
									subItems.add(subItem);
								}
							}
						}
						testItem.setTestSubItems(subItems);
						testItems.add(testItem);
					}
				}
			}
			
			TestItemConfig.INSTANCE.init(testItems);
		}
	}
}
